/**
 * @note 属性拷贝
 * @param {Object} target[ 最终返回拷贝后的对象 ]
 * @param {Object} obj [ 需要获取属性的对象 ]
 * @param {Array} attrs [ 需要拷贝的属性 ]
 * */
export function objProCopy(target,obj,attrs){
    var i,
        iLen,
        j,
        jLen,
        pro,
        exPro,//扩展后的属性名
        items=[],
        recursion = false,
        argArr=[];
    //如果参数大于3个
    if(arguments.length > 3){
        recursion = true;
        argArr = Array.prototype.slice.call(arguments,0);
        target = argArr[0];
        obj = argArr.slice(1,-1);
        attrs = argArr[argArr.length-1];
    }
    //递归拷贝属性，从前往后
    if(recursion){
        for(i = 0,iLen = obj.length;i < iLen;i++){
            for(j =0,jLen = attrs.length;j < jLen;j++){
                pro = exPro = attrs[j];
                if(attrs[j].indexOf('-') != -1){
                    items = attrs[j].split('-');
                    pro = items[0];
                    exPro = items[1];
                }
                if(obj[i].hasOwnProperty(pro)){
                    target[exPro]=obj[i][pro];
                }
            }
        }
    }else{
        console.log(attrs);
        for(i = 0,iLen = attrs.length;i < iLen;i++){
            pro = exPro = attrs[i];
            if(attrs[i].indexOf('-') != -1){
                items = attrs[i].split('-');
                pro = items[0];
                exPro = items[1];
            }
            if(obj.hasOwnProperty(pro)){
                target[exPro]=obj[pro];
            }
        }
    }
    return target;
}

/**
 * @note 对象序列化
 * @param {Object} obj... [ 序列化的对象 ]
 * @param {Array} attrs [ 对象属性 ]
 * */
export function objToString(obj, attrs) {
    /*
        @param  
            1. obj 对象
            2. attrs 需要转换的属性
     */
    var result = [],
        i,
        iLen,
        j,
        jLen,
        pro,
        exPro,
        items,
        recursion = false,
        argArr = [];
    //如果参数大于3个
    if(arguments.length > 2) {
        recursion = true;
        argArr = Array.prototype.slice.call(arguments, 0);
        obj = argArr.slice(0, -1);
        attrs = argArr[argArr.length - 1];
    }
    //递归
    if(recursion) {
        for(i = 0, iLen = obj.length; i < iLen; i++) {
            for(j = 0, jLen = attrs.length; j < jLen; j++) {
                //获取属性
                pro = exPro = attrs[j];
                if(attrs[j].indexOf('-') != -1) {
                    items = attrs[j].split('-');
                    pro = items[0];
                    exPro = items[1];
                }
                //mapGetters 时，属性名在原型上，所以不能使用  hasOwnProperty
                if(pro in obj[i]) {
                    result.push(encodeURIComponent(exPro) + '=' + encodeURIComponent(obj[i][pro]));
                }
            }
        }
    } else {
        for(i = 0, iLen = attrs.length; i < iLen; i++) {
            //获取属性
            pro = exPro = attrs[i];
            if(attrs[i].indexOf('-') != -1) {
                items = attrs[i].split('-');
                pro = items[0];
                exPro = items[1];
            }
            //mapGetters 时，属性名在原型上，所以不能使用  hasOwnProperty
            if(pro in obj) {
                result.push(encodeURIComponent(exPro) + '=' + encodeURIComponent(obj[pro]));
            }
        }
    }
    return result.join('&');
}

/**
 * @note 获取查询对象
 * @return {Object}
 * */
export function getQueryStringArgs() {
    var qs = (location.search.length > 0) ? location.search.substring(1) : '',
        args = {},
        items = qs.length ? qs.split('&') : [],
        item = null,
        name = null,
        value = null,
        i = 0,
        len = items.length;
    for(i = 0; i < len; i++) {
        item = items[i].split('=');
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);
        if(name.length) {
            args[name] = value;
        }
    }
    return args;
}

/**
 * @note 省市区三级组合
 * @return {Object}
 * @param {Array} collection [{code:110000,name:'北京市'}[,{...}]] 
 * @param {String} divide [ 分隔符 ]
 * @return {String} {Default ''}
 * */
export function collectionJoin(collection,divide = '/'){
    /*
        
    */
    let result = [];
    for(let i = 0,len = collection.length;i < len;i++){
        result.push(collection[i].name);
    }
    return result.join(divide);
}

/**
 * @note 节流函数
 * param {Function} method [ 需要被节流函数 ]
 * param {Object} context [ 函数的执行环境绑定 ]
 * */
export function throttle(method,context = null,THROTTLE_TIME = 1000){
    let arg=Array.prototype.slice.call(arguments,3);
    clearTimeout(method.tId);
    method.cid = setTimeout(function(){
        method.apply(context,arg);
    },THROTTLE_TIME);
}

/**
 * @note 序列化
 * param {Function} method [ 需要被节流函数 ]
 * param {Object} context [ 函数的执行环境绑定 ]
 * */
export function serialize(obj){
    let result = [];
    Object.keys(obj).forEach(key => {
        result.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
    });
    return result.join('&');
}
